package com.chen.shopping_order_customer_api.controller;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chen.shopping_common.pojo.Address;
import com.chen.shopping_common.pojo.Area;
import com.chen.shopping_common.pojo.City;
import com.chen.shopping_common.pojo.Province;
import com.chen.shopping_common.result.BaseResult;
import com.chen.shopping_common.service.AddressService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/address")
public class AddressController {

    @DubboReference
    private AddressService addressService;

    //查询所有省份
    @GetMapping("/findAllProvince")
    public BaseResult<List<Province>> findAllProvince(){
        List<Province> allProvince = addressService.findAllProvince();
        return BaseResult.ok(allProvince);
    }
    //查询省份下的所有城市
    @GetMapping("/findCityByProvince")
    public BaseResult<List<City>> findAllCity(Long provinceId){
        List<City> allCity = addressService.findCityByProvince(provinceId);
        return BaseResult.ok(allCity);
    }
    //查询城市下的所有地区
    @GetMapping("/findAreaByCity")
    public BaseResult<List<Area>> findAllArea(Long cityId){
        List<Area> allArea = addressService.findAreaByCity(cityId);
        return BaseResult.ok(allArea);
    }
    //新增地址
    @PostMapping("/add")
    public BaseResult add(@RequestHeader Long userId,@RequestBody Address address){
        address.setUserId(userId);
        addressService.add(address);
        return BaseResult.ok();
    }
    //修改地址
    @PutMapping("/update")
    public BaseResult update(@RequestHeader Long userId,@RequestBody Address address){
        address.setUserId(userId);
        addressService.update(address);
        return BaseResult.ok();
    }
    //根据id查询地址
    @GetMapping("/findById")
    public BaseResult<Address> findAddressById(Long id){
        Address address = addressService.findById(id);
        return BaseResult.ok(address);
    }
    //删除地址
    @DeleteMapping("/delete")
    public BaseResult delete(Long id){
        addressService.delete(id);
        return BaseResult.ok();
    }

    //查询用户下的所有地址
    @GetMapping("/findByUser")
    public BaseResult<List<Address>> findAllByUser(@RequestHeader Long userId){
        List<Address> allAddressByUser = addressService.findAllAddressByUser(userId);
        return BaseResult.ok(allAddressByUser);
    }
}
